#!/usr/bin/python
# -*- coding: utf-8 -*-
import clocks
import comm_channels
import ckpt_protocols
import random
import filesystem
import lambda_algorithm
import services
import applications
import sys

from os import path

def main():
    local_dict = {'HOSTS': {},
                  'FILESYSTEM_ARGS': {},
                  'LAMBDA_ARGS': {},
                  'APP_ARGS': {},
                  'LOG_ENABLED': False }
    execfile(path.expanduser('~/.cheops.py'), {}, local_dict)
    globals().update(local_dict)
    pid = int(sys.argv[1])
    channel = comm_channels.TCP_comm_channel(pid, HOSTS, DISPATCHER)
    clock = clocks.lamport_clock(channel)
    fs = filesystem.FileSystemAF(FS, **FILESYSTEM_ARGS)
    args = [PROTOCOL, clock, fs]
    if PROTOCOL == 'RDT_DISKLESS_FDAS':
        p_ring = services.virtualring()
        p_ring.set_comm(clock)
        alg = lambda_algorithm.SingleParity(**LAMBDA_ARGS)
        args.extend([alg, p_ring])
    
    srv = ckpt_protocols.CkptProtocolsAF(*args)
    srv.log = LOG_ENABLED

    app = applications.ApplicationAF(APP, protocol=srv, 
                                     **APP_ARGS)
    
    app.Run()

    srv.close()

if __name__ == "__main__":
    main()

